﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PersonProject;
using System.Threading;

namespace Person_Form_Socket
{
    public partial class PersonAppSocket : Form
    {
        public static iDS ds = null;
        public static List<Person> pList;
        
        public PersonAppSocket()
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;  
            InitializeComponent();
              
        }

        public void setDS(iDS DS)
        {
            ds = DS;
        }

        private void PersonApp_Load(object sender, EventArgs e)
        {
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;  
            if (ds == null)
            {
                ds = new DS_WCFClient();              
            }
            Load_Btn_Click(this, new EventArgs());
        }

        public void Load_Btn_Click(object sender, EventArgs e)
        {
            if (ds == null)
            {
                ds = new DS_WCFClient();
            }
            pList = ds.Load();
            DataGrid.DataSource = pList;      
        }

        private void DataGrid_CellDoubleClick(object sender, EventArgs e)
        {
            if (DataGrid.SelectedCells[0].OwningRow.Cells[0].Value != null)
            {
                string str = DataGrid.SelectedCells[0].OwningRow.Cells[0].Value.ToString();
                EditForm edit = new EditForm(this);
                edit.command = "Edit";
                edit.id = Convert.ToInt32(str);
                edit.Show();
                edit.CheckAndFill();
            }
            else
            {
                Btn_new_Click(sender, e);
            }
        }

        private void Btn_new_Click(object sender, EventArgs e)
        {
            EditForm edit = new EditForm(this);
            edit.command = "New";
            edit.Show();
            edit.EmptyFill();
        }

        private void Delete_btn_Click(object sender, EventArgs e)
        {
            if (DataGrid.SelectedCells[0].OwningRow.Cells[0].Value != null)
            {
                string str = DataGrid.SelectedCells[0].OwningRow.Cells[0].Value.ToString();
                int id = Convert.ToInt32(str);
                ds.Delete(id);
                Load_Btn_Click(this, new EventArgs());
            }
        }

    }
}
